package com.pan.admin.module.api.controller;

import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.common.utils.poi.ExcelUtil;
import com.pan.admin.module.api.entity.ApiRequiredRule;
import com.pan.admin.module.api.service.ApiRequiredRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * api必填字段规则Controller
 *
 * @author ts
 * @date 2023-10-30
 */
@RestController
@RequestMapping("/api/rule")
public class ApiRequiredRuleController extends BaseController {
    @Autowired
    private ApiRequiredRuleService apiRequiredRuleService;

    /**
     * 查询api必填字段规则列表
     */
    @PreAuthorize("@ss.hasPermi('api:keys:query')")
    @GetMapping("/list")
    public TableDataInfo list(ApiRequiredRule apiRequiredRule) {
//        startPage();
        List<ApiRequiredRule> list = apiRequiredRuleService.list(apiRequiredRule);
        return getDataTable(list);
    }

    /**
     * 导出api必填字段规则列表
     */
    @PreAuthorize("@ss.hasPermi('api:rule:export')")
    @Log(title = "api必填字段规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ApiRequiredRule apiRequiredRule) {
        List<ApiRequiredRule> list = apiRequiredRuleService.list(apiRequiredRule);
        ExcelUtil<ApiRequiredRule> util = new ExcelUtil<ApiRequiredRule>(ApiRequiredRule.class);
        util.exportExcel(response, list, "api必填字段规则数据");
    }

    /**
     * 获取api必填字段规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:rule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(apiRequiredRuleService.getById(id));
    }

    /**
     * 新增api必填字段规则
     */
    @PreAuthorize("@ss.hasPermi('api:rule:add')")
    @Log(title = "api必填字段规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ApiRequiredRule apiRequiredRule) {
        return toAjax(apiRequiredRuleService.insertApiRequiredRule(apiRequiredRule));
    }

    /**
     * 修改api必填字段规则
     */
    @PreAuthorize("@ss.hasPermi('api:rule:edit')")
    @Log(title = "api必填字段规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ApiRequiredRule apiRequiredRule) {
        return toAjax(apiRequiredRuleService.updateApiRequiredRule(apiRequiredRule));
    }

    /**
     * 删除api必填字段规则
     */
    @PreAuthorize("@ss.hasPermi('api:rule:remove')")
    @Log(title = "api必填字段规则", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(apiRequiredRuleService.batchDeleteById(ids));
    }
}
